//
//  WelcomeViewController.swift
//  weibo1
//
//  Created by Apple on 15/11/29.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        //设置用户头像
        iconimage.sd_setImageWithURL(UserAccountViewModel().url, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation(){
        //偏移的大小根据屏幕的高度+180
        let offset = -ScreenH + 180
        iconimage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
            
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.5, options: [], animations: { () -> Void in
            //刷新页面
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
//                print("OK")
                 NSNotificationCenter.defaultCenter().postNotificationName(rootViewController, object: nil)
        }
        
    }
    
    
    
    private  func setupUI(){
        view.addSubview(iconimage)
        view.addSubview(textlabel)
        //设置约束
        iconimage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        textlabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconimage.snp_centerX)
            make.top.equalTo(iconimage.snp_bottom).offset(16)
        }
        //设置圆角
        iconimage.layer.cornerRadius = 45
        iconimage.layer.masksToBounds = true
    }
    
    //懒加载子视图
    private lazy var backImage :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconimage :UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var textlabel :UILabel = {
        let lab = UILabel()
        lab.text = (UserAccountViewModel().name ?? "") + " 欢迎归来"
        lab.textColor = UIColor.redColor()
        lab.font = UIFont.systemFontOfSize(16)
        lab.sizeToFit()
        return lab
        }()
    
}
